#include "extractorSuperpixelDataSet.h"
#include "extractorConcatenateVector.h"
#include "siftVocabulary.h"
#include "colorVocabulary.h"
#include "configurationParameters.h"
#include "slic.h"

extractorSuperpixelDataSet::extractorSuperpixelDataSet(string directory, string file_name_format, int fromFile, int toFile) {
	// create dictionary for color
	colorVocabulary colorVocabulary(configurationParameters::sizeLabColorVocabulary(), configurationParameters::sizeLabColorSample());
	colorVocabulary.getSampleOnTrainingSet("flower/", "image", fromFile, toFile);
	colorVocabulary.generateDictionary();
	Mat colorDictionary = colorVocabulary.getDictionary();
	// create dictionary for SIFT
	siftVocabulary siftvoca(configurationParameters::sizeSiftVocabulary(), configurationParameters::sizeSiftSample());
	siftvoca.getSampleOnTrainingSet("flower/","image", fromFile, toFile);
	siftvoca.generateDictionary();
	Mat siftDictionary = siftvoca.getDictionary();

	for (int i = fromFile; i <= toFile; i++){
		string order_file = "";
		if (i < 10){
			order_file = "000" + std::to_string(i);

		}
		if (10 <= i & i < 100){
			order_file = "00" + std::to_string(i);

		}
		if (100 <= i & i < 1000){
			order_file = "0" + std::to_string(i);
		}
		if (1000 <= i & i < 10000){
			order_file = std::to_string(i);
		}

		string string_path_file_image = directory + file_name_format + "_" + order_file + ".jpg";

		char *cstr_image = new char[string_path_file_image.length() + 1];
		strcpy(cstr_image, string_path_file_image.c_str());
		/* Load image */
		IplImage *img = cvLoadImage(cstr_image);
		Mat t_image = cvCloneImage(img);
		IplImage *lab_image = cvCloneImage(img);
		cvCvtColor(img, lab_image, CV_BGR2Lab);

		/* Yield the number of superpixels and weight-factors from the user. */
		int w = img->width, h = img->height;
		int nr_superpixels = 180; //250;// 100 // 200; // Maximum K  cluster
		int nc = 100; //100; // 40
		double step = sqrt((w * h) / (double)nr_superpixels); // S = sqrt(N/K) , S: size of superpixels
		/* Perform the SLIC superpixel algorithm. */
		Slic slic;
		slic.generate_superpixels(lab_image, step, nc);
		slic.create_connectivity(lab_image);

		extractorConcatenateVector extractorConcatenateVector(t_image, cstr_image, slic, siftDictionary, colorDictionary);

		vector<concatenateVector> tempListVector = extractorConcatenateVector.getListVector();
		
		for (int i = 0; i < tempListVector.size(); ++i) {
			vector<double> tempVector = tempListVector.at(i).getVectorValue();
			int size = tempVector.size();
			cv::Mat_<double> tempFeatureVector(1, size);
			for (int i = 0; i < size; ++i) {
				tempFeatureVector(0, i) = tempVector.at(i);
			}
			sampleFeatureVector.push_back(tempFeatureVector);
			label.push_back(tempListVector.at(i).getLabelForegroundBackground());
		}
		
		delete[] cstr_image;
	}
	std::cout << sampleFeatureVector.rows << std::endl;
	std::cout << label.rows << std::endl;
}